package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.changgou.util.StringComUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/1/14 21:15
 * @Description: 品牌服务类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        //Null数据不保存
        brandMapper.insertSelective(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        bindExample(example,searchMap);
        return brandMapper.selectByExample(example);
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        //过滤非法参数
        if(page<0){
            page = 1;
        }
        if(size<0){
            size = 20;
        }
        //开启分页插件
        PageHelper.startPage(page,size);

        return (Page<Brand>) brandMapper.selectAll();
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //过滤非法参数
        if(page<=0){
            page = 1;
        }
        if(size<=0){
            size = 20;
        }
        //开启分页插件
        PageHelper.startPage(page,size);
        Example example = new Example(Brand.class);
        bindExample(example,searchMap);
        return (Page<Brand>) brandMapper.selectByExample(example);
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.selectByCategoryName(categoryName);
    }

    /**
     * 数据绑定
     * @param example
     * @param searchMap
     */
    private void bindExample(Example example,Map<String, Object> searchMap){
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            //品牌名
            if(!StringComUtils.isEmpty((String) searchMap.get("name"))){
                criteria.andLike("name","%" + (String) searchMap.get("name") + "%");
            }
            //品牌首字母
            if(!StringComUtils.isEmpty((String) searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
    }

}
